home *** CD-ROM | disk | FTP | other *** search
/ Programming Sound Cards / Programming Sound Cards.iso / sound_70 / speaker.c < prev    next >
Text File  |  1995-01-01  |  3KB  |  134 lines

  1.  
  2. //------------------------------------------------------------------------------
  3. // Copyright (c) David Welch, 1993
  4. //------------------------------------------------------------------------------
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <dos.h>
  9. #include <mem.h>
  10. #define inb inportb
  11. void buildcode ( unsigned short );
  12.  
  13. //OPCODES FOUND FROM DEBUG:
  14. //E461          IN      AL,61
  15. //24FC          AND     AL,FC
  16. //88C4          MOV     AH,AL
  17. //80CC02        OR      AH,02
  18. //C3            RET
  19. //CB            RETF
  20. //BA6100        MOV     DX,0061
  21. //B502          MOV     CH,02
  22. //B100          MOV     CL,00
  23. //8AC5          MOV     AL,CH
  24. //EE            OUT     DX,AL
  25. //8AC1          MOV     AL,CL
  26. //90            NOP
  27.  
  28. unsigned char opstart[] = {0xBA,0x61,0x00,0xB5,0x02,0xB1,0x00};
  29. unsigned char ophigh[] = {0x8A,0xC5,0xEE};
  30. unsigned char oplow[] = {0x8A,0xC1,0xEE};
  31. unsigned char opend[] = {0xCB};
  32.  
  33. typedef (*fun) ( void );
  34. fun playfuncs[256];
  35.  
  36. unsigned short ra;
  37. unsigned short rb;
  38. unsigned long la;
  39. unsigned short bps;
  40. FILE *fp;
  41. unsigned char *data;
  42. //------------------------------------------------------------------------------
  43. void main ( int argc, char *argv[] )
  44. {
  45.     opstart[6]=inb(0x61)&0xFC;
  46.     opstart[4]=opstart[6]|2;
  47.     if(argc==1)
  48.     {
  49.         printf("ss filename [bps]\n");
  50.         exit(1);
  51.     }
  52.     bps=50;
  53.     if(argc>2)
  54.     {
  55.         bps=atoi(argv[2]);
  56.     }
  57.     if((fp=fopen(argv[1],"rb"))==NULL)
  58.     {
  59.         printf("Error opening file [%s]\n",argv[1]);
  60.         exit(1);
  61.     }
  62.     fseek(fp,0,2);
  63.     la=ftell(fp);
  64.     fseek(fp,0,0);
  65.     if(la>65000) la=65000;
  66.     if((data=malloc(la))==NULL)
  67.     {
  68.         printf("Out of memory\n");
  69.         exit(1);
  70.     }
  71.     fread(data,1,la,fp);
  72.     fclose(fp);
  73.     buildcode(bps);
  74.     for(rb=0;rb<la;rb++)
  75.     {
  76.         playfuncs[data[rb]]();
  77.     }
  78. }
  79. //------------------------------------------------------------------------------
  80. void buildcode ( unsigned short n )
  81. {
  82.     unsigned char *p;
  83.     unsigned short value, sum, i;
  84.     unsigned short opstartlen, ophighlen, oplowlen, opendlen;
  85.     unsigned long size;
  86.  
  87.     opstartlen=sizeof(opstart);
  88.     ophighlen=sizeof(ophigh);
  89.     oplowlen=sizeof(oplow);
  90.     opendlen=sizeof(opendlen);
  91.  
  92.     size=opstartlen+opendlen;
  93.     if(size>65535)
  94.     {
  95.         printf("Error opcodes too long\n");
  96.         exit(1);
  97.     }
  98.     if(ophighlen>oplowlen) size+=n*ophighlen; else size+=n*oplowlen;
  99.  
  100.     for(value=0;value<256;value++)
  101.     {
  102.         p=calloc(1,size);
  103.         if(p==NULL)
  104.         {
  105.             printf("Out of memory\n");
  106.             exit(1);
  107.         }
  108.         playfuncs[value]=(fun)p;
  109.         memcpy(p,opstart,opstartlen); p+=opstartlen;
  110.         sum=0;
  111.         for(i=0;i<bps;++i)
  112.         {
  113.             sum+=value;
  114.             if(sum>=256)
  115.             {
  116.                 sum-=255;
  117.                 memcpy(p,ophigh,ophighlen); p+=ophighlen;
  118.             }
  119.             else
  120.             {
  121.                 if(i)
  122.                 {
  123.                     memcpy(p,oplow,oplowlen); p+=oplowlen;
  124.                 }
  125.             }
  126.         }
  127.         memcpy(p,opend,opendlen);
  128.     }
  129. }
  130. //------------------------------------------------------------------------------
  131. // Copyright (c) David Welch, 1993
  132. //------------------------------------------------------------------------------
  133.  
  134.